//
//  SquareTableViewCell.m
//  Findif
//
//  Created by Mac on 2019/1/23.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "SquareTableViewCell.h"

// 最大高度限制
//CGFloat maxLimitHeight = 0;

@interface SquareTableViewCell ()

// 背景
@property (nonatomic, weak) UIView * backView;
// 头像
@property (nonatomic, weak) UIImageView * headImageView;
// 名称
@property (nonatomic, weak) UILabel * nameLab;
// 内容
@property (nonatomic, weak) UILabel * contentLabel;
// 时间
@property (nonatomic, weak) UILabel * timeLabel;
// 性别
@property (nonatomic, weak) UIImageView * genderImageView;
// 社团
@property (nonatomic, weak) UIView * groupView;
// 地址图片
@property (nonatomic, weak) UIImageView * localImageView;
// 地址文字
@property (nonatomic, weak) UIButton * localLabel;
// 点赞
@property (nonatomic, strong) UIButton * likeButton;
// 评论
@property (nonatomic, strong) UIButton * commitButton;

@property (nonatomic, weak)UIImageView * menuImageView;

@property (nonatomic, weak) UIView * lineView;

@property (nonatomic, weak)UILabel * groupLabel;
@property (nonatomic, weak)UIImageView * rightImageView;
@property (nonatomic, copy)NSString * ID;
@property (nonatomic, copy)NSString * user_id;

@end

@implementation SquareTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.userInteractionEnabled = YES;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    // 头像视图
    UIImageView * headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * SCREENH6, kBlank, kFaceWidth, kFaceWidth)];
    headImageView.contentMode = UIViewContentModeScaleAspectFill;
//    headImageView.userInteractionEnabled = YES;
    headImageView.layer.cornerRadius = 20 * SCREENH6;
    headImageView.layer.masksToBounds = YES;
    UITapGestureRecognizer * headGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headTapAction)];
    headImageView.userInteractionEnabled = YES;
    [headImageView addGestureRecognizer:headGR];
    
    self.headImageView = headImageView;
    
    [self.backView addSubview:self.headImageView];
    // 名字视图
    UILabel * nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.headImageView.right + 10 * SCREENH6, self.headImageView.top, kTextWidth, 15 * SCREENH6)];
    nameLabel.font = [UIFont boldSystemFontOfSize:15.0];
    nameLabel.textColor = ZRBlackColor;
    nameLabel.backgroundColor = ZRWhiteColor;
    
    self.nameLab = nameLabel;
    
    [self.backView addSubview:self.nameLab];
    
    // 性别视图
    UIImageView * genderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.nameLab.left, self.nameLab.bottom + 5 * SCREENH6, 13 * SCREENH6, 13 * SCREENH6)];
    genderImageView.contentMode = UIViewContentModeScaleAspectFill;
    //    headImageView.userInteractionEnabled = YES;
    
    self.genderImageView = genderImageView;
    
    [self.backView addSubview:self.genderImageView];
    
    
    UIButton * muneButton = [[UIButton alloc] initWithFrame:CGRectMake(320 * SCREENH6, 15 * SCREENH6, 25 * SCREENH6, 18 * SCREENH6)];
    
    [muneButton setImage:[UIImage imageNamed:@"更多"] forState:UIControlStateNormal];
    [muneButton addTarget:self action:@selector(clickMoreButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.muneButton = muneButton;
    
    [self.backView addSubview:self.muneButton];
    
    
    UIButton * deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(310 * SCREENH6, 15 * SCREENH6, 21 * SCREENH6, 21 * SCREENH6)];
    
    [deleteButton setImage:[UIImage imageNamed:@"动态删除"] forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(clickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
    deleteButton.hidden = YES;
    
    self.deleteButton = deleteButton;
    
    [self.backView addSubview:self.deleteButton];
    
    UILabel * contentLabel = [[UILabel alloc] init];
    
    contentLabel.textColor = ZRBlackColor;
    contentLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    contentLabel.numberOfLines = 0;
    
    self.contentLabel = contentLabel;
    
    [self.backView addSubview:self.contentLabel];
    
    // 图片区
    _imageListView = [[MMImageListView alloc] initWithFrame:CGRectZero];
    [self.backView addSubview:_imageListView];
    
    //社团
    UILabel * groupLabel = [UILabel new];
    
    groupLabel.textColor =ZRWhiteColor;
    groupLabel.backgroundColor = ZRBlackColor;
    groupLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    groupLabel.layer.cornerRadius = 5 * SCREENH6;
    groupLabel.layer.masksToBounds = YES;
    groupLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [groupLabel addGestureRecognizer:tapGR];
    groupLabel.hidden = YES;
    
    self.groupLabel = groupLabel;
    
    [self.backView addSubview:self.groupLabel];
    
    // 社团视图
    UIImageView * rightImageView = [[UIImageView alloc] init];
    rightImageView.contentMode = UIViewContentModeScaleAspectFill;
    rightImageView.image = [UIImage imageNamed:@"右边小三角"];
    rightImageView.hidden = YES;
    //    headImageView.userInteractionEnabled = YES;
    
    self.rightImageView = rightImageView;
    
    [self.groupLabel addSubview:self.rightImageView];
    
    // 地址视图
    UIImageView * localImageView = [[UIImageView alloc] init];
    localImageView.contentMode = UIViewContentModeScaleAspectFill;
    localImageView.image = [UIImage imageNamed:@"广场place"];
    //    headImageView.userInteractionEnabled = YES;
    localImageView.hidden = YES;
    
    self.localImageView = localImageView;
    
    [self.backView addSubview:self.localImageView];
    
    
    UIButton * localLabel = [UIButton new];
    
    [localLabel setTitleColor:ColorWithRGB(0x4C87D8) forState:UIControlStateNormal];
    [localLabel addTarget:self action:@selector(clickLocalButton) forControlEvents:UIControlEventTouchUpInside];
    localLabel.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    localLabel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    localLabel.hidden = YES;
    
    self.localLabel = localLabel;
    
    [self.backView addSubview:self.localLabel];
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe4e4e4);
    
    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = ColorWithRGB(0x909090);
    timeLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.timeLabel = timeLabel;
    
    [self.backView addSubview:self.timeLabel];
    
    
    self.likeButton = [UIButton new];
    
    [self.likeButton setImage:[UIImage imageNamed:@"点赞-未选中"] forState:UIControlStateNormal];
    [self.likeButton setImage:[UIImage imageNamed:@"点赞2"] forState:UIControlStateSelected];
    [self.likeButton setTitle:@" 0" forState:UIControlStateNormal];
    self.likeButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [self.likeButton setTitleColor:ColorWithRGB(0x4B4B4B) forState:UIControlStateNormal];
    [self.likeButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.backView addSubview:self.likeButton];
    
    self.commitButton = [UIButton new];
    
    [self.commitButton setImage:[UIImage imageNamed:@"回复"] forState:UIControlStateNormal];
    [self.commitButton setTitle:@" 0" forState:UIControlStateNormal];
    self.commitButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [self.commitButton setTitleColor:ColorWithRGB(0x4B4B4B) forState:UIControlStateNormal];
    [self.commitButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.backView addSubview:self.commitButton];
}


- (void)setModel:(SquareModel *)model{
    
    _model = model;
    
    self.ID = [NSString stringWithFormat:@"%@",model.id];
    self.user_id = [NSString stringWithFormat:@"%@",model.user_id];
//    NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.user_head_img];
//    NSURL * url = [NSURL URLWithString:iconStr];
    // 头像
//    [self.headImageView sd_setImageWithURL:url];
    if([model.user_head_img containsString:@"http"]) {
        
        NSURL * url = [NSURL URLWithString:model.user_head_img];
        [self.headImageView sd_setImageWithURL:url];
    } else {
        
        NSString * iconStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,model.user_head_img];
        NSURL * url = [NSURL URLWithString:iconStr];
        [self.headImageView sd_setImageWithURL:url];
    }
    
    // 昵称
    self.nameLab.text = [NSString stringWithFormat:@"%@",model.user_name];
    [self.nameLab sizeToFit];
    int gender = [model.user_sex intValue];
    if (gender == 1) {
        
        self.genderImageView.image = [UIImage imageNamed:@"男性"];
    }else if (gender == 2){
        
       self.genderImageView.image = [UIImage imageNamed:@"女性"];
    }
    CGFloat bottom = self.headImageView.bottom + kPaddingValue;
    CGFloat rowHeight = 0;
    NSString * content = model.dynamic_content;
    if (content) {
        
        self.contentLabel.text = content;
        CGSize attrStrSize = [self preferredSizeWithMaxWidth:325 * SCREENH6 withLabel:self.contentLabel];
        CGFloat labH = attrStrSize.height;
        
        self.contentLabel.frame = CGRectMake(self.headImageView.left, bottom, attrStrSize.width, labH);
        
        bottom = self.contentLabel.bottom + kPaddingValue;
    }
    // 图片
    _imageListView.model = model;
    NSArray *array = [model.dynamic_img componentsSeparatedByString:@","];
    //    NSLog(@"count===%lu",(unsigned long)array.count);
    if (array.count > 0) {
        _imageListView.origin = CGPointMake(self.headImageView.left, bottom);
        bottom = _imageListView.bottom + kPaddingValue;
    }
    
    if (model.group_name) {
        
        self.groupLabel.hidden = NO;
        self.rightImageView.hidden = NO;
        NSString * name = [NSString stringWithFormat:@" %@",model.group_name];
        self.groupLabel.text = name;
        CGSize attrStrSize = [self preferredSizeWithMaxWidth:325 * SCREENH6 withLabel:self.groupLabel];
        self.groupLabel.frame = CGRectMake(self.headImageView.left, bottom, attrStrSize.width + 20 * SCREENH6, attrStrSize.height + 5 * SCREENH6);
        
        self.rightImageView.frame = CGRectMake(self.groupLabel.width - 8 * SCREENH6, 7 * SCREENH6, 3 * SCREENH6, 6 * SCREENH6);
        
        bottom = self.groupLabel.bottom + kPaddingValue;
    }
    
    
    if (model.dynamic_address) {
        
        self.localLabel.hidden = NO;
        self.localImageView.hidden = NO;
        
        self.localImageView.frame = CGRectMake(self.headImageView.left, bottom, 15 * SCREENH6, 18 * SCREENH6);
//        self.localLabel.text = model.dynamic_address;
        [self.localLabel setTitle:model.dynamic_address forState:UIControlStateNormal];
        self.localLabel.frame = CGRectMake(self.localImageView.right + 5 * SCREENH6, bottom + 2 * SCREENH6, 240 * SCREENH6, 13 * SCREENH6);
        
        bottom = self.localImageView.bottom + kPaddingValue;
    }
    
    self.lineView.frame = CGRectMake(self.headImageView.left, bottom, 325 * SCREENH6, 1 * SCREENH6);
    
    bottom = self.lineView.bottom + kPaddingValue;
    
    self.timeLabel.frame = CGRectMake(self.headImageView.left, bottom + 8 * SCREENH6, 150 * SCREENH6, 12 * SCREENH6);
    self.timeLabel.text = [NSString stringWithFormat:@"%@",model.dynamic_inputtime];
    
    self.commitButton.frame = CGRectMake(280 * SCREENH6, bottom + 5 * SCREENH6, 55 * SCREENH6, 20 * SCREENH6);
    self.likeButton.frame = CGRectMake(225 * SCREENH6, bottom + 5 * SCREENH6, 55 * SCREENH6, 20 * SCREENH6);
    NSString * praise_number = [NSString stringWithFormat:@" %@",model.praise_number];
    [self.likeButton setTitle:praise_number forState:UIControlStateNormal];
    NSString * message_board_number = [NSString stringWithFormat:@" %@",model.message_board_number];
    [self.commitButton setTitle:message_board_number forState:UIControlStateNormal];
    
    int praise_status = [model.praise_status intValue];
    if (praise_status == 1) {
        
        self.likeButton.selected = YES;
    }else{
        
        self.likeButton.selected = NO;
    }
    
    bottom = self.likeButton.bottom + kPaddingValue;
    
    
    rowHeight = bottom + kBlank;
    
    self.backView.frame = CGRectMake(15 * SCREENH6, 10 * SCREENH6, 345 * SCREENH6, rowHeight);
    // 这样做就是起到缓存行高的作用，省去重复计算!!!
    
    _model.rowHeight = rowHeight + kBlank;
}

- (void)clickLocalButton{
    
    NSString * lon = [NSString stringWithFormat:@"%@",self.model.dynamic_longitude];
    NSString * lat = [NSString stringWithFormat:@"%@",self.model.dynamic_latitude];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickLocalButtonLocal:WithDynamic_latitude:WithDynamic_longitude:)]) {
        
        [self.delegate clickLocalButtonLocal:self.model.dynamic_address WithDynamic_latitude:lat WithDynamic_longitude:lon];
    }
}

- (void)clickMoreButton:(UIButton *)button{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickItemButton: WithCell:)])
    {
        [self.delegate clickItemButton:button WithCell:self];
    }
}

- (CGSize)preferredSizeWithMaxWidth:(CGFloat)maxWidth withLabel:(UILabel *)label
{
    
    CGSize size = [label sizeThatFits:CGSizeMake(maxWidth, 10000000.0f)];
    size.width = fmin(size.width, maxWidth); //在numberOfLine为1模式下返回的可能会比maxWidth大，所以这里我们限制下
    return size;
}

- (UIButton *)setupButtonWithImage:(NSString *)img WithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setImage:[UIImage imageNamed:@"点赞-未选中"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"点赞2"] forState:UIControlStateSelected];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [button setTitleColor:ColorWithRGB(0x4B4B4B) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

- (void)clickBottomButton:(UIButton *)button{
    
    if (button == self.likeButton) {
        
        if (button.selected) {
            
            [[ZRNetworkTool sharedTool] deleteDynamicPraiseWithID:self.model.id WithBlock:^(BOOL success, id responseData) {
                
                if (success) {
                    
                    button.selected = NO;
                    NSString * title = button.titleLabel.text;
                    int likeCount = [title intValue];
                    int praise_number = [self.model.praise_number intValue];
                    
                    
                    if (likeCount == 0) {
                        
                        likeCount = 0;
                        praise_number = 0;
                    }else{
                        
                        likeCount = likeCount - 1;
                        praise_number = praise_number - 1;
                    }
                    
                    NSString * res = [NSString stringWithFormat:@" %d",likeCount];
                    self.model.praise_status = @"0";
                    self.model.praise_number = [NSString stringWithFormat:@"%d",praise_number];
                    [button setTitle:res forState:UIControlStateNormal];
                }
            }];
        }else{
            
            [[ZRNetworkTool sharedTool] initiateDynamicPraiseWithID:self.model.id WithBlock:^(BOOL success, id responseData) {
                
                if (success) {
                    
                    button.selected = YES;
                    NSString * title = button.titleLabel.text;
                    int likeCount = [title intValue];

                    likeCount = likeCount + 1;
                    int praise_number = [self.model.praise_number intValue];
                    praise_number = praise_number + 1;
                    self.model.praise_number = [NSString stringWithFormat:@"%d",praise_number];
                    self.model.praise_status = @"1";
     
                    NSString * res = [NSString stringWithFormat:@" %d",likeCount];
                    
                    [button setTitle:res forState:UIControlStateNormal];
                }
            }];
            
        }
    }
}

- (void)tapAction{

    if (self.delegate && [self.delegate respondsToSelector:@selector(clickGroupLabelWithCell:)])
    {
        [self.delegate clickGroupLabelWithCell:self];
    }
}

- (void)clickDeleteButton{

    if (self.delegate && [self.delegate respondsToSelector:@selector(clickDeleteButtonWith:)])
    {
        [self.delegate clickDeleteButtonWith:self.ID];
    }
}

- (void)headTapAction{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickHeadImageViewWith:)])
    {
        [self.delegate clickHeadImageViewWith:self.user_id];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
